using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.Schedule;
namespace Janus.NorthwindDemo2007
{
	public partial class ScheduleView
	{

		protected override void OnDataProviderChanged()
		{
			base.OnDataProviderChanged();
			this.Schedule.SetDataBinding(this.DataSource, "Appointments");
		}

		public override FolderView[] GetDefinedViews()
		{
			return new FolderView[] {new FolderView("Day/Week/Month", ContentViewType.ScheduleView, "")};

		}

		public override int ItemCount
		{
			get
			{
				return this.Schedule.GetAppointmentsFromRange(this.Schedule.DateRange).Length;
			}
		}
		public override bool Printable
		{
			get
			{
				return true;
			}
		}
		public override void PrintPreview(Form ownerForm)
		{
			frmPrintPreview preview = new frmPrintPreview();
			preview.Show(this.printDocument, ownerForm);
		}

		public override void Print()
		{
			try
			{
				this.printDocument.Print();
			}
			catch (Exception exc)
			{
				MessageBox.Show(exc.Message, NorthwindApp.MessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		public override object CurrentItem
		{
			get
			{
				if (this.Schedule.SelectedAppointments.Count > 0)
				{
					return this.Schedule.SelectedAppointments[0];
				}
				else
				{
					return null;
				}
			}
		}
		public override bool CanDelete
		{
			get
			{
				if (this.CurrentItem != null & this.Schedule.AllowDelete)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		public override bool CanEdit
		{
			get
			{
				if (this.CurrentItem != null & this.Schedule.AllowEdit)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
		public override void Delete()
		{
			int i = 0;
			for (i = this.Schedule.SelectedAppointments.Count - 1; i >= 0; i--)
			{
				this.Schedule.SelectedAppointments[i].Delete();
			}
		}
		public override Janus.Windows.UI.CommandBars.UICommandManager ControlCommandManager
		{
			get
			{
				return commandManager;
			}
		}


		private void Schedule_SelectedAppointmentsChanged(object sender, System.EventArgs e)
		{
			OnCurrentItemChanged(EventArgs.Empty);
		}

		private void Schedule_AddingAppointment(object sender, Janus.Windows.Schedule.AddAppointmentEventArgs e)
		{
			if (e.AddAction == AddAction.DoubleClick)
			{
				e.Cancel = true;
			}

		}

		private void schedule1_RemovingRecurrentAppointment(object sender, Janus.Windows.Schedule.RemovingRecurrentAppointmentEventArgs e)
		{
			RecurrenceOptionDialog frm = new RecurrenceOptionDialog();
			frm.ShowRecurrenceOptionDialog(e.RecurrentAppointment.Text, true);
			if (frm.DialogResult == DialogResult.OK)
			{
				e.DeleteTheSeries = frm.ApplyToTheSeries;
			}
			else
			{
				e.Cancel = true;
			}
		}

		public override bool UpdateChanges()
		{
			this.Schedule.EndEdit();
			return true;
		}

		private ReminderForm mReminderForm;
		public ReminderForm ReminderForm
		{
			get
			{
				return mReminderForm;
			}
			set
			{
				if (mReminderForm != value)
				{
					if (mReminderForm != null)
					{
                        mReminderForm.Closed -= new System.EventHandler(ReminderForm_Closed);
						mReminderForm.Dispose();
					}
					mReminderForm = value;
                    if (mReminderForm != null)
                    {
                        mReminderForm.Closed += new System.EventHandler(ReminderForm_Closed);
                    }

				}
			}
		}

		private void ReminderForm_Closed(object sender, EventArgs e)
		{
			ReminderForm = null;
		}

		private void Schedule_Reminder(object sender, Janus.Windows.Schedule.AppointmentEventArgs e)
		{
			if (ReminderForm == null)
			{
				ReminderForm = new ReminderForm();
				ReminderForm.ShowReminderForm(Schedule, e.Appointment);
				ReminderForm.Focus();
				ReminderForm.Update();
			}
			else
			{
				ReminderForm.AddItem(e.Appointment);
				ReminderForm.Update();
			}
			ReminderForm.BringToFront();

		}

		public override void SaveSettings()
		{

			ConfigDataSet.ContentViewSettingsRow viewRow = NorthwindApp.ConfigDataSet.ContentViewSettings.FindByViewName(this.GetType().FullName);

			if (viewRow == null)
			{
				viewRow = NorthwindApp.ConfigDataSet.ContentViewSettings.AddContentViewSettingsRow(this.GetType().FullName, "");
			}
			viewRow.BeginEdit();
			System.IO.MemoryStream stream = new System.IO.MemoryStream();
			this.Schedule.SaveLayoutFile(stream);
			viewRow.Layout = System.Convert.ToBase64String(stream.ToArray());
			stream.Close();
			viewRow.EndEdit();
		}
		public override void LoadSettings()
		{
			//Load saved layout if the application is reading layouts and 
			//if it exist in the config dataset

			if (NorthwindApp.ReadSettingsFromDS)
			{
				ConfigDataSet.ContentViewSettingsRow viewRow = NorthwindApp.ConfigDataSet.ContentViewSettings.FindByViewName(this.GetType().FullName);

				if (viewRow != null)
				{
					if (! viewRow.IsLayoutNull() && viewRow.Layout.Length > 0)
					{
						System.IO.MemoryStream stream = new System.IO.MemoryStream(System.Convert.FromBase64String(viewRow.Layout));
						this.Schedule.LoadLayoutFile(stream);
						stream.Close();
					}
				}
			}
		}

		public override string StatusBarInfo
		{
			get
			{
				if (this.Schedule.Dates.Count > 1)
				{
					return this.Schedule.DateRange.Start.ToString("MMMM dd") + " - " + this.Schedule.DateRange.End.ToString("MMMM dd");
				}
				else
				{
					return this.Schedule.Date.ToString("MMMM dd");
				}
			}
		}

		private void Schedule_DatesChanged(object sender, System.EventArgs e)
		{

			if (NorthwindApp.MainForm != null)
			{
				//MainForm can be null if ScheduleView is being used in 
				NorthwindApp.MainForm.ResetStatusBarInfo();
			}

		}

		protected override void OnDockChanged(System.EventArgs e)
		{

		}

        public override Janus.Windows.Ribbon.Ribbon RibbonControl
        {
            get
            {
                return this.ribbon1;
            }
        }

        private void Schedule_AppointmentChanged(object sender, AppointmentChangeEventArgs e)
        {

        }
	}

} //end of root namespace